<?php
namespace app\common\service\UserService;

use app\model\BbcModel\Member;
use app\model\BbcModel\MemberFeedback;
use app\model\BbcModel\Store;
use app\model\BbcModel\StoreMember;
use app\Normal;
use think\facade\Request;

class MemberFeedbackService {

    //每页行数
    protected $row = 10;

    /**
     * 用户反馈列表
     * @param array $param
     * @return void
     * @author huj
     */
    public function index(array $param = []) {

        $model = MemberFeedback::instance();
        // if (Request::has('nickname', 'param', true) && !(Request::has('mobile', 'param', true))) {
        //     $nicknameMids = StoreMember::whereLike('nick_name', "%" . $param['nickname'] . "%")->column('mid');
        //     $model        = $model->whereIn('mid', $nicknameMids);
        // } elseif (!(Request::has('nickname', 'param', true)) && Request::has('mobile', 'param', true)) {
        //     $mobileMids = Member::where('mobile', $param['mobile'])->column('mid');
        //     $model      = $model->whereIn('mid', $mobileMids);
        // } elseif (Request::has('nickname', 'param', true) && Request::has('mobile', 'param', true)) {
        //     $nicknameMids = StoreMember::whereLike('nick_name', "%" . $param['nickname'] . "%")->column('mid');
        //     $mobileMids   = Member::where('mobile', $param['mobile'])->column('mid');
        //     $mixMids      = array_intersect($nicknameMids, $mobileMids);
        //     $model        = $model->whereIn('mid', $mixMids);
        // };
        if (Request::has('nickname', 'param', true)) {
            $model = $model->whereLike('nick_name', "%" . $param['nickname'] . "%");
        }
        if (Request::has('mobile', 'param', true)) {
            $mobileMids = Member::where('mobile', $param['mobile'])->column('mid');
            $model      = $model->whereIn('mid', $mobileMids);
        }

        if (Request::has('status', 'param', true) && in_array($param['status'], ['1', '2'])) {
            $model = $model->where('status', $param['status']);
        };
        if (Request::has('start_time', 'param', true)) {
            $model = $model->where('cd', '>=', $param['start_time']);
        };
        if (Request::has('end_time', 'param', true)) {
            $model = $model->where('cd', '<=', $param['end_time']);
        };
        if (Request::has('limit', 'param', true)) {
            $this->row = Request::param('limit');
        }
        $fields = [
            'id', 'mid', 'mn_sid', 'status', 'content', 'cd', 'nick_name as nickname'
        ];
        $result = $model->field($fields)->order(['cd' => 'desc', 'id' => 'desc'])->paginate($this->row, false, ['query' => Request::param()]);
        if (!$result->isEmpty()) {
            $appendField = ['status_text', 'cd_text'];
            $result->append($appendField);
            $mids         = $result->column('mid');
            $mnids        = $result->column('mn_sid');
            // $nicknameInfo = StoreMember::whereIn('mid', $mids)->whereIn('mn_sid', $mnids)->field(['mid', 'mn_sid', 'nick_name'])->select();
            // $nicknameInfo = StoreMember::whereIn('mid', $mids)->field(['mid', 'mn_sid', 'nick_name'])->select();
            $mobileInfo   = Member::whereIn('mid', $mids)->column('mobile', 'mid');
            $storeInfo    = Store::whereIn('id', $mnids)->where('status', 1)->where('check_status', 1)->where('is_pay', 1)->column('store_name', 'id');
            foreach ($result->items() as $key => $value) {
                // $value->nickname = '';
                // foreach ($nicknameInfo as $v) {
                //     if ($value->mid == $v->mid && $value->mn_sid == $v->mn_sid) {
                //         $value->nickname = $v->nick_name;
                //     }elseif ($value->mid == $v->mid){
                //         $value->nickname = $v->nick_name;
                //     }
                // }
                $value->mobile     = ($mobileInfo[$value->mid]) ?? '';
                $value->store_name = ($storeInfo[$value->mn_sid]) ?? '';
            }
        }
        return Normal::returnCode(Normal::SUC, "请求成功", ['total' => $result->total(), 'list' => $result->items()]);
    }

    /**
     * 操作受理狀態
     * @param array $param
     * @return void
     * @author huj
     */
    public function handleStatus(array $param = []) {
        $validate = \think\facade\Validate::rule([
            'id' => ['require', 'regex' => '/^[1-9]\d*$/'],
        ]);
        if (!$validate->check($param)) {
            return Normal::returnCode(Normal::FAIL, $validate->getError());
        }

        $feedback = MemberFeedback::where('id', $param['id'])->field(['id', 'status'])->find();
        if (!$feedback) {
            return Normal::returnCode(Normal::FAIL, "反馈信息不存在");
        }
        if ($feedback->status != MemberFeedback::HANDLING) {
            return Normal::returnCode(Normal::FAIL, "该条反馈信息已处理");
        }
        $feedback->status = MemberFeedback::HANDLED;
        $result           = $feedback->save();
        if ($result) {
            return Normal::returnCode(Normal::SUC, "受理成功");
        }
        return Normal::returnCode(Normal::FAIL, "受理失败");
    }

}